package cn.zmwh.im.server.modules.ums.service;

import cn.hutool.core.collection.CollUtil;
import cn.zmwh.im.server.api.Result;
import cn.zmwh.im.server.modules.ums.mapper.ImGroupMapper;
import cn.zmwh.im.server.modules.ums.pojo.entity.ImGroup;
import cn.zmwh.im.server.modules.ums.pojo.entity.ImGroupMember;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 群 服务实现类
 * </p>
 *
 * @author dmzmwh
 * @since 2025-06-19
 */
@Service
public class ImGroupService extends ServiceImpl<ImGroupMapper, ImGroup> {

    @Autowired
    ImGroupMemberService imGroupMemberService;

    public Result listByUid(Long uid) {
        List<ImGroupMember> list = imGroupMemberService.list(Wrappers.<ImGroupMember>lambdaQuery()
                .eq(ImGroupMember::getUid, uid));
        if (CollUtil.isEmpty(list)) {
            return Result.success();
        }
        Set<Long> groupIds = list.stream().map(ImGroupMember::getGroupId).collect(Collectors.toSet());
        List<ImGroup> imGroups = listByIds(groupIds);

        return Result.ok(imGroups);
    }
}
